﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using SmartProject.Data;
using SmartProject.Exceptions;

namespace SmartProject
{
    public abstract class BusinessRulePreference
    {
        private readonly DataHelper _dataHelper = new DataHelper();
        private string _preferenceName;
        private BusinessRule[] _businessRules;

        protected void Initialize(string preferenceName, BusinessRule[] businessRules)
        {
            _preferenceName = preferenceName;
            _businessRules = businessRules;
        }

        /// <summary>
        /// Gets the name of this preference.
        /// </summary>
        public string Name
        {
            get { return _preferenceName; }
        }

        private int _id;
        private string _value;

        public void GetValue()
        {
            _dataHelper.Initialize();
            _dataHelper.Add("PrefName", _preferenceName, 100);
            System.Collections.Hashtable preference =
                _dataHelper.Load("smartproject_GetPreference");
            _value = (string)preference["PrefValue"];
            _id = Convert.ToInt32(preference["PreferenceId"]);
        }

        public void Save(string preferenceValue)
        {
            GetValue();
            bool isNew = String.IsNullOrEmpty(_value);
            _dataHelper.Initialize();
            _dataHelper.Add("PrefValue", preferenceValue, 200);
            if (isNew)
            {
                _dataHelper.Add("PrefName", _preferenceName, 100);
                _dataHelper.Run("smartproject_AddPreference");
            }
            else
            {
                _dataHelper.Add("PreferenceId", _id);
                _dataHelper.Run("smartproject_UpdatePreference");
            }
        }

        /// <summary>
        /// If you have multiple business rules to choose from, this method
        /// will pick the business rule that you have already selected during
        /// your initial setup.
        /// </summary>
        /// <returns></returns>
        public BusinessRule GetSelectedBusinessRule()
        {
            GetValue();
            string value = _value;
            foreach (BusinessRule businessRule in _businessRules)
            {
                if (businessRule.Name == value)
                {
                    return businessRule;
                }
            }

            return null;
        }

        /// <summary>
        /// Access the business rules stored by the preference type.
        /// </summary>
        public BusinessRule[] BusinessRules
        {
            get { return _businessRules; }
        }

        /// <summary>
        /// Gets whether this preference is used ONLY in initial setup. Some
        /// preferences may be configured after the initial setup/ install.
        /// </summary>
        public abstract bool IsInitialSetupOnly { get; }
    }
}
